package com.atguigu.day04;

import com.atguigu.beans.WaterSensor;
import com.atguigu.func.WaterSensorMapFunction;
import javassist.compiler.ast.Variable;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: china
 * @create: 2024-04-02 20:02
 * @description:
 **/
public class Flink07_Connect_InnerJoin {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        SingleOutputStreamOperator<WaterSensor> ds1 = env.socketTextStream("hadoop102", 8888)
                .map(new WaterSensorMapFunction());
        SingleOutputStreamOperator<WaterSensor> ds2 = env.socketTextStream("hadoop102", 8889)
                .map(new WaterSensorMapFunction());

        ConnectedStreams<WaterSensor, WaterSensor> connectDS = ds1.connect(ds2);

        ConnectedStreams<WaterSensor, WaterSensor> keyByCS = connectDS.keyBy(WaterSensor::getId, ws2 -> ws2.getId());

        SingleOutputStreamOperator<String> processDS = keyByCS.process(
                new CoProcessFunction<WaterSensor, WaterSensor, String>() {
                    Map<String, List<WaterSensor>> map1Cache = new HashMap<>();
                    Map<String, List<WaterSensor>> map2Cache = new HashMap<>();

                    @Override
                    public void processElement1(WaterSensor waterSensor, CoProcessFunction<WaterSensor, WaterSensor, String>.Context context, Collector<String> collector) throws Exception {
                        String id = waterSensor.getId();
                        if (map1Cache.containsKey(id)) {
                            map1Cache.get(id).add(waterSensor);
                        } else {
                            ArrayList<WaterSensor> wslist = new ArrayList<>();
                            wslist.add(waterSensor);
                            map1Cache.put(id, wslist);
                        }

                        if(map2Cache.containsKey(id)) {
                            for (WaterSensor ws : map2Cache.get(id)) {
                                collector.collect(waterSensor.toString() + "&" + ws);
                            }
                        }

                    }

                    @Override
                    public void processElement2(WaterSensor waterSensor, CoProcessFunction<WaterSensor, WaterSensor, String>.Context context, Collector<String> collector) throws Exception {
                        String id = waterSensor.getId();
                        if (map2Cache.containsKey(id)) {
                            map2Cache.get(id).add(waterSensor);
                        } else {
                            ArrayList<WaterSensor> wslist = new ArrayList<>();
                            wslist.add(waterSensor);
                            map2Cache.put(id, wslist);
                        }

                        if(map1Cache.containsKey(id)) {
                            for (WaterSensor ws : map1Cache.get(id)) {
                                collector.collect(waterSensor.toString() + "&" + ws);
                            }
                        }
                    }
                }
        );

        processDS.print();
        env.execute();


    }
}
